package com.blocks.gui;

import com.blocks.ScriptBlocksApplication;
import com.blocks.script.ScriptUtils;

import de.lessvoid.nifty.EndNotify;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.controls.console.ConsoleControl;
import de.lessvoid.nifty.controls.textfield.TextFieldControl;
import de.lessvoid.nifty.effects.EffectEventId;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.KeyInputHandler;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class TerminalScreenController implements ScreenController {
	Nifty nifty;
	private Element textField;
	private Element chatArea;
	private Screen screen;
	@Override
	public void bind(Nifty nifty, Screen screen) {
		this.nifty = nifty;
		textField = screen.findElementByName("chat_input");
		chatArea = screen.findElementByName("chat_area");
		this.screen = screen;

	    textField.addInputHandler(new KeyInputHandler() {
			@Override
			public boolean keyEvent(NiftyInputEvent inputEvent) {
				if(inputEvent == null) return false;
				switch(inputEvent) {
				case SubmitText:
					System.out.println("SUBMITTEXT");
					sendMessage();
					return true;
				}
				return false;
			}
		});

	    
	}

	@Override
	public void onEndScreen() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStartScreen() {

	}


	public void sendMessage() {
		String textValue = screen.findNiftyControl("chat_input", TextField.class).getText();
		//String textValue = textField.getNiftyControl(TextField.class).getText(); //Use findNiftyControl and just TextField.class.
		//ScriptBlocksApplication.getInstance().sendMessage(textField.getControl(TextFieldControl.class).getText());
		ScriptBlocksApplication.getInstance().sendMessage(textValue);
		//chatArea.getNiftyControl(TextFieldControl.class).setText(chatArea.getNiftyControl(TextFieldControl.class).getText()+"\n"+textValue);
		String oldChat = screen.findNiftyControl("chat_area", TextField.class).getText();
		screen.findNiftyControl("chat_area", TextField.class).setText(oldChat+"\n"+textValue);
	}
	public void backToGame() {
		
		ScriptBlocksApplication.getInstance().gameState();
		nifty.exit();		
	}


    
}
